package com.csiec.clock.view.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.csiec.clock.adpter.MyClockAdapter;
import com.csiec.clock.bean.MyClock;
import com.csiec.clock.bean.clock.BackData;
import com.csiec.clock.bean.clock.RealData;
import com.csiec.clock.callback.ClockCallBack;
import com.csiec.clock.databinding.ActivityMyClockBinding;
import com.csiec.clock.model.ClockModel;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.json.JSONException;

import java.util.List;


public class MyClockViewModel extends ViewModel {

    private final String TAG = MyClockViewModel.class.getSimpleName();

    @SuppressLint("StaticFieldLeak")
    private Activity activity;
    private ActivityMyClockBinding binding;

    private ClockModel model;
    private final android.os.Handler handler = new Handler();
    private List<MyClock> clockList;

    public void init(Activity activity, ActivityMyClockBinding binding) {

        model = new ClockModel();
        this.activity = activity;
        this.binding = binding;
        getClock();
    }

    //获取用户自建打卡列表
    public void getClock() {
        model.getClock(new ClockCallBack() {

            @Override
            public void onSuccess(BackData backData) {
                if (backData.getStatus() == 200) {
                    clockList = backData.getData().getData();
                    handler.post(() -> initView());
                }
            }

            @Override
            public void onFailed() {
                handler.post(() -> {
                    showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "网络异常");
                });
            }
        });

    }

    private void initView() {
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(activity.getApplication()));
        if (clockList != null) {
            MyClockAdapter adapter = new MyClockAdapter(clockList, activity);
            binding.recyclerView.setAdapter(adapter);
        }
    }

    //提示悬浮窗
    public void showTip(int type, String msg) {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(type)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        binding.recyclerView.postDelayed(tipDialog::dismiss, 2000);
    }

}
